function isLogin() {
  return JSON.parse(localStorage.getItem('token'))
}

function renderTopbar(user) {
  let photo = document.getElementById('topbar-photo')
  let currentUsername = document.getElementById('currentUsername')
  let exit = document.getElementById('exit')
  console.log(user)
  photo.src = user.photo
  currentUsername.innerHTML = user.username
  exit.onclick = function () {
    localStorage.removeItem('token')
    location.href = '/views/index.html'
  }
}
function renderSidemenu(user, id) {
  let highlight = document.querySelector('#' + id)
  highlight.style.color = '#0d6efd'
  if (user.role !== 'admin') {
    // useritem.removeItem()
    document.querySelector('#useritem').remove()
  }
}

async function load(id) {
  let user = isLogin()
  if (user) {
    let topbarText = await fetch(
      '/views/admin/components/topbar/topbar.html'
    ).then((res) => res.text())
    document.querySelector('.topbar').innerHTML = topbarText
    renderTopbar(user)
    let sidemenu = await fetch(
      '/views/admin/components/sidemenu/sidemenu.html'
    ).then((res) => res.text())
    document.querySelector('.sidemenu').innerHTML = sidemenu
    renderSidemenu(user, id)
  } else {
    location.href = '/views/index.html'
  }
}

export { load, isLogin }
